package com.hxy.travel.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hxy.travel.constant.MessageConstant;
import com.hxy.travel.entity.PageResult;
import com.hxy.travel.entity.QueryPageBean;
import com.hxy.travel.entity.Result;
import com.hxy.travel.pojo.TravelGroup;
import com.hxy.travel.pojo.TravelItem;
import com.hxy.travel.service.TravelGroupService;
import com.hxy.travel.service.TravelItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travelgroup")
public class TravelGroupController {

     @Reference
     private TravelGroupService travelGroupService;

     @Reference
     private TravelItemService travelItemService ;


    @PutMapping("/add.do")
    public Result add( @RequestBody TravelGroup travelGroup ,Integer[] travelItemIds){

        System.out.println(travelItemIds);

        travelGroupService.addTravelGroupAndIds(travelGroup,travelItemIds);

        return Result.ok(MessageConstant.ADD_TRAVELGROUP_SUCCESS);

    }
    @PostMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult list = travelGroupService.findPage(queryPageBean);

        return list;

    }
//    @GetMapping("/delete.do")
//    public Result delete(Integer id){
//
//        try {
//            travelItemService.deleteTravelItemById(id);
//            return Result.ok(MessageConstant.DELETE_TRAVELITEM_SUCCESS);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.ok(MessageConstant.DELETE_TRAVELITEM_FAIL);
//        }
//
//    }
    @GetMapping("/selectById.do")
    public Result selectById(Integer id){

      TravelGroup travelGroup=travelGroupService.selectById(id);

      return Result.data(travelGroup);

    }

    @GetMapping("/selectTravelItemByGroupId.do")
    public Result selectTravelItemByGroupId( Integer id ) {
        List<Integer> list = travelItemService.selectTravelItemByGroupId(id);
        return Result.data(list);
    }

    @PostMapping("/edit.do")
    public Result edit( @RequestBody TravelGroup travelGroup ,Integer[] travelItemIds ) {

        travelGroupService.editTravelGroup(travelGroup, travelItemIds);

        return Result.ok(MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
    }
    @GetMapping("/delete.do")
    public Result delete( Integer id ){
        try {
            travelGroupService.deleteTravelItemById(id);
        } catch ( RuntimeException e ){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } catch ( Exception e) {
            e.printStackTrace();
            return Result.ok(MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
        return Result.fail(MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
    }
    @GetMapping("/findAll.do") public Result findAll(){
        List<TravelGroup> list = travelGroupService.findAll();
        return Result.data(list,MessageConstant.QUERY_TRAVELGROUP_SUCCESS);
    }


//    @PostMapping("/edit.do")
//    public Result edit(@RequestBody TravelItem travelItem){
//
//        travelItemService.editTravelItem(travelItem);
//
//
//        return Result.ok(MessageConstant.EDIT_TRAVELITEM_SUCCESS);
//
//    }
//
    @GetMapping("/list.do")
    public Result list(){
        List<TravelItem> list =travelGroupService.selectTravelGroupList();

        return Result.data(list,MessageConstant.QUERY_TRAVELGROUP_SUCCESS);

    }
}
